﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_ProcecureMaterialController : BaseApiController
    {
        #region DI

        public PB_ProcecureMaterialController(IPB_ProcecureMaterialBusiness pB_ProcecureMaterialBus)
        {
            _pB_ProcecureMaterialBus = pB_ProcecureMaterialBus;
        }

        IPB_ProcecureMaterialBusiness _pB_ProcecureMaterialBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_ProcecureMaterial>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_ProcecureMaterialBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_ProcecureMaterial> GetTheData(IdInputDTO input)
        {
            return await _pB_ProcecureMaterialBus.GetTheDataAsync(input.id);
        }
        [HttpPost]
        public async Task<List<PB_ProcecureMaterialDTO>> GetDataListByArea(string MaterialId)
        {
            var res = await _pB_ProcecureMaterialBus.GetDataListAsyncs(MaterialId);
            return res;
        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(Entity.DTO.PB_ProcecureMaterialsDTO data)
        {
            //先删除原有的PB_ProcecureMaterials的所有数据
            var where = LinqHelper.True<PB_ProcecureMaterial>();
            if (!data.ProcecureId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ProcecureMaterial, bool>(
                        ParsingConfig.Default, false, $@"ProcecureId.Equals(@0)", data.MaterialId);
                where = where.And(newWhere);
            }
            await _pB_ProcecureMaterialBus.DeleteOtherIdDataAsync(where);
            //插入新的PB_ProcecureMaterials
            if (data.Id.IsNullOrEmpty())
            {

                foreach (var item in data.ProcecureId)
                {

                    PB_ProcecureMaterial material = new PB_ProcecureMaterial();
                    material.Id = data.Id;
                    material.MaterialId = data.MaterialId;
                    material.ProcecureId = item.ToString();
                    material.Deleted = false;
                    InitEntity(material);
                    await _pB_ProcecureMaterialBus.AddDataAsync(material);
                }

            }
            else
            {
                //修改PB_ProcecureMaterial
                foreach (var item in data.ProcecureId)
                {
                    PB_ProcecureMaterial material = new PB_ProcecureMaterial();
                    material.Id = data.Id;
                    material.MaterialId = data.MaterialId;
                    material.ProcecureId = item.ToString();
                    await _pB_ProcecureMaterialBus.UpdateDataAsync(material);
                }
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_ProcecureMaterialBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}